#include "G2D.h"

G2D::G2D():Graphics()
{
	screen = NULL;
	text = NULL;
	font = NULL;
}

SDL_Rect G2D::convertQuad(Quad q)
{
	SDL_Rect r = SDL_Rect();
	r.x = q.x;
	r.y = q.y;
	r.w = q.w;
	r.h = q.h;
	return r;
}

SDL_Surface* G2D::getScreen()
{
	return screen;
}

SDL_Surface* G2D::getText()
{
	return text;
}

SDL_Surface* G2D::loadImage(std::string filename)
{
	SDL_Surface *loadedImage = NULL;
	SDL_Surface *optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());

	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);

		SDL_FreeSurface(loadedImage);
	}

	return optimizedImage;
}

void G2D::applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, clip, destination, &offset);
}

void G2D::applyScreen(int x, int y, SDL_Surface* source, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, clip, screen, &offset);
}

void G2D::setColorKeyT(SDL_Surface *surface, Uint32 pixel)
{
	if(surface != NULL)
	{
		SDL_SetColorKey(surface, SDL_SRCCOLORKEY, pixel);
	}
}

Uint32 G2D::getPixel(SDL_Surface *surface, int x, int y)
{
	Uint32 *pixels = (Uint32 *)surface->pixels;

	return pixels[(y * surface->w) + x];
}

void G2D::setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
	Uint32 *pixels = (Uint32 *)surface->pixels;

	pixels[(y * surface->w) + x] = pixel;
}

SDL_Surface* G2D::flipImage(SDL_Surface *source, int flags)
{
	SDL_Surface *flippedimage = NULL;

	if(source->flags & SDL_SRCCOLORKEY)
	{
		flippedimage = SDL_CreateRGBSurface( SDL_SWSURFACE, source->w, source->h, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, 0 );
	}
	else
	{
		flippedimage = SDL_CreateRGBSurface( SDL_SWSURFACE, source->w, source->h, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, source->format->Amask );
	}

	if(SDL_MUSTLOCK(source))
	{
		SDL_LockSurface(source);
	}

	for(int x = 0, rx = flippedimage->w-1; x < flippedimage->w; x++, rx--)
	{
		for(int y = 0, ry = flippedimage->h-1; y < flippedimage->h; y++, ry--)
		{
			 Uint32 pixel = getPixel( source, x, y );

			 if( ( flags & FLIP_VERTICAL ) && ( flags & FLIP_HORIZONTAL ) )
			 {
				 setPixel( flippedimage, rx, ry, pixel );
			 }
			 else if( flags & FLIP_HORIZONTAL )
			 {
				 setPixel( flippedimage, rx, y, pixel );
			 }
			 else if( flags & FLIP_VERTICAL )
			 {
				 setPixel( flippedimage, x, ry, pixel );
			 }
		}
	}

	if(SDL_MUSTLOCK(source))
	{
		SDL_UnlockSurface(source);
	}

	if(source->flags & SDL_SRCCOLORKEY)
	{
		SDL_SetColorKey(flippedimage, SDL_RLEACCEL | SDL_SRCCOLORKEY, source->format->colorkey);
	}

	return flippedimage;
}

void G2D::printScreen(SDL_Surface *screen, float x, float y, std::string s)
{
	SDL_Color color = {0,0,0};
	SDL_FreeSurface(text);
	text = TTF_RenderText_Solid(font, s.c_str(), color);
	applySurface(x, y, text, screen, NULL);
}

void G2D::setFont(std::string filename, int fontsize)
{
	font = TTF_OpenFont(filename.c_str(), fontsize);
	if(!font)
	{
		exit(EXIT_FAILURE);
	}
}

void G2D::drawRect(int x, int y, int w, int h, Uint32 color)
{
	SDL_Rect r = SDL_Rect();
	r.x = x;
	r.y = y;
	r.w = w;
	r.h = h;
	SDL_FillRect(screen, &r, color);
}

void G2D::drawRect(SDL_Rect rect, Uint32 color)
{
	SDL_FillRect(screen, &rect, color);
}

Uint32 G2D::getColor(Uint8 r, Uint8 g, Uint8 b)
{
	return SDL_MapRGB(screen->format, r, g, b);
}
